package com.jt.yygh.hospital.dict.controller;

import com.jt.yygh.common.result.Result;
import com.jt.yygh.hospital.dict.service.DictService;
import com.jt.yygh.hospital.entity.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author jiang tao
 * @date 2022/2/18
 * @description:
 **/

@RequestMapping("/admin/dict")
@RestController
@Api("字典管理")
public class DictController {

    @Autowired
    private  DictService dictService;

    @RequestMapping(value = "/exportDictData", method = RequestMethod.GET)
    @ApiOperation("导出数据字典")
    public void exportDict(HttpServletResponse response){
        dictService.exportDictData(response);
    }

    @RequestMapping(value = "importDictData", method = RequestMethod.POST)
    @ApiOperation("导入字典数据")
    public Result<Boolean> importDictData(MultipartFile file){
        dictService.importDictData(file);
        return Result.success(true);
    }

    @RequestMapping(value = "findByDictCode/{dictCode}", method = {RequestMethod.GET})
    @ApiOperation("根据dictCode获取下级节点")
    public Result<Dict[]> findByDictCode(@PathVariable String dictCode){
        List<Dict> list = dictService.findByDictCode(dictCode);
        Dict[] dicts = new Dict[list.size()];
        for (int i = 0; i < list.size(); i++) {
            dicts[i] = list.get(i);
        }
        return Result.success(dicts);
    }

    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping(value = "findChildData/{id}")
    public Result<Dict[]> findChildData(@PathVariable Long id) {
        List<Dict> list = dictService.findChildData(id);
        Dict[] dicts = new Dict[list.size()];
        for (int i = 0; i < list.size(); i++) {
            dicts[i] = list.get(i);
        }
        return Result.success(dicts);
    }

    @ApiOperation("根据dictcode和value查询 对应的name 字段")
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode,
                          @PathVariable String value) {
        String dictName = dictService.getDictName(dictCode,value);
        return dictName;
    }

    @ApiOperation("根据value找到对应的name 值")
    @GetMapping("getName/{value}")
    public String getName(@PathVariable String value) {
        String dictName = dictService.getDictName("",value);
        return dictName;
    }






}
